
import { createWebHistory, createRouter } from 'vue-router'

const routes = [
  {
    path: "/",
   redirect: "/login",
    component: ()=>import('../views/IndexView.vue'),
    children: [
      {
        path: "overview",
        component: () => import("../views/OverView.vue"),
      },
      {
        path: "localfile",
        component: () => import("../views/FileView.vue"),
      },
      {
        path: "article",
        component: () => import("../views/ArticleView.vue"),
      },
      {
        path: "gallery",
        component: () => import("../views/GalleryView.vue"),
      },
      {
        path: "diary",
        component: () => import("../views/DiaryView.vue"),
      },
      {
        path: "setting",
        component: () => import("../views/SettingView.vue"),
      },

    ],
  },
  {
    path: "/editgallery",
    component: () => import("../views/EditGalleryView.vue"),
  },
  {
    path: "/login",
    component: () => import("../views/LoginView.vue"),
  },
  {
    path: "/editarticle",
    component: () => import("../views/EditArticleView.vue"),
  },
  {
    path: "/register",
    component: () => import("../views/RegisterView.vue"),
  },
  {
    path: "/mdedit",
    component: () => import("../views/MarkdowmEditView.vue"),
  },
  {
    path: "/errorinfo",
    component: () => import("../views/error/404.vue"),
  }
];

const router = createRouter({
  history: createWebHistory(),
  routes,
})

//在路由请求前进行的拦截
router.beforeEach((to, from, next) => {
  if (to.matched.length === 0) { 
    //如果地址错误或未匹配到路由
    from.name ? next({ name: from.name }) : next('/errorinfo') 
  }//如果上级也未匹配到路由则跳转登录页面，如果上级能匹配到则转上级路由
  else { next(); }
})

export default router;